package com.test.daily.leetcode.y2021.m10.day1018;

/**
 * @descriptions: FindCompletement
 * @author: Tom
 * @date: 2021/10/18 上午 08:37
 * @version: 1.0
 */
public class Code01_FindCompletement {
    public static void main(String[] args) {
        Code01_FindCompletement cf = new Code01_FindCompletement();
        System.out.println(cf.findComplement2(5));
    }

    public int findComplement(int num) {
        int highBit = 0;
        for (int i = 1; i <= 30; i++) {
            if (num >= 1 << i) {
                highBit = i;
            } else {
                break;
            }
        }
        int exor = highBit == 30 ? 0x7fffffff:(1 <<(highBit + 1)) - 1;
        return num ^ exor;
    }
    public int findComplement2(int num) {
        int ret = num;
        ret |= ret >> 1;
        ret |= ret >> 2;
        ret |= ret >> 4;
        ret |= ret >> 8;
        ret |= ret >> 16;
        return num ^ ret;

    }

}
